package com.example.config;

import com.example.filter.MyFilter;
import com.example.interceptor.LoginInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: 21050134
 * @date: 2023/12/11 15:48
 * @description:
 */
@Configuration
public class BaseConfiguration implements WebMvcConfigurer {

	// 将这个类交给spring管理。不使用注解的方式，因为使用注解，需要在源码上@Component
	// 使用方法，返回@Bean
	// FilterRegistrationBean 这个类，是第三方的，我们不能加@Component
	@Bean
	public FilterRegistrationBean<MyFilter> getFilter(){
		FilterRegistrationBean<MyFilter> filter = new FilterRegistrationBean<MyFilter>(new MyFilter());
		return filter;

	}


	@Override
	public void addInterceptors(InterceptorRegistry registry){
		InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginInterceptor());
		// 哪些请求被拦截
		// 注意，这里是两个*
		interceptorRegistration.addPathPatterns("/**");
		// 哪些请求不被拦截
        // interceptorRegistration.excludePathPatterns();

	}
}
